# -*- coding: utf-8 -*-
# @project: huawang-api20230914
# @file： read_yaml.py
# @Time：2023/7/17 15:26
# @Author：liaogan
import time
import yaml
import os

from common.log.log import log
from config.conf import Test_data, TEST_CASE_DIR


def get_excel_yaml(filder, alldata=True):
    """默认就是取的caseDate，不填写则取返回值"""
    reslist = []
    """把文件从磁盘加载到内存---打开"""
    fo = open(filder, "r", encoding="utf-8")
    """使用yaml读取"""
    res = yaml.load(fo, Loader=yaml.FullLoader)
    # print(res)
    # 返回数据样式---根据项目来----数据驱动要什么类型就封装什么类型！
    for CaseDate in res:
        reslist.append((CaseDate['title'], CaseDate['url'], CaseDate['data'], CaseDate['resp']))
    if alldata:
        return res
    else:
        return reslist


def get_yamls_data(filder):
    reslist = []
    fo = open(filder, "r", encoding="utf-8")
    res = yaml.load_all(fo, Loader=yaml.FullLoader)
    for one in res:
        reslist.append(one)
    return reslist


def get_yaml_data(filder):
    fo = open(filder, "r", encoding='utf-8')
    res = yaml.load(fo, Loader=yaml.FullLoader)
    return res


def get_time(Y=False, M=False):
    if Y:
        """"获取当前：年、月、日、时、分、秒"""
        return str(
            time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()).replace('-', '').replace(':', '').replace(" ", ''))
    elif M:
        """获取当前：年、月、日"""
        return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

    else:
        """获取当前、时、分、秒"""
        return time.strftime("%H:%M:%S", time.localtime())


def read_yaml_data(yaml_file_path):
    try:
        with open(yaml_file_path, 'r') as yaml_file:
            yaml_data = yaml.safe_load(yaml_file)
        return yaml_data
    except Exception as e:
        print("发生错误：", e)
        return None


import yaml
from pprint import pp
def get_yaml_data1(yaml_file):
    with open(yaml_file,encoding='utf-8') as f:
        return yaml.safe_load(f.read())

if __name__ == '__main__':
    print(Test_data)
    # data = read_yaml_data('D:\huawang-api20230914\data\product_detail.yaml')
    # log.info(data)
    # productValue = data['data']['productValue']
    # attrValues = list(productValue.keys())[0]
    # log.info(attrValues)


    # print(PollyPath.configs_path)
    # pp(get_yaml_data(PollyPath.configs_path / 'allelements.yaml')['LoginPage'])
    # print(get_yaml_data1(os.path.join(Test_data,'yk_allelements.yaml')))
    # case_data=get_yaml_data1(os.path.join(Test_data,'login_all.yaml'))
    # for one in case_data:
    #     print(one)


    # case_data=get_yaml_data1(os.path.join(TEST_CASE_DIR,'test_huawang_yk/test_ht_ui/test_fun_module/login_all.yaml'))
    # for one in case_data:
    #     print(one)

    case_data=get_yaml_data1('//data/yk_allelements.yaml')['LoginPage']
    print(case_data)
    for one in case_data:
        print(one)

    # productId = data['data']['productValue'][attrValues]['productId']
    # log.info(productId)
    # id = data['data']['productValue'][attrValues]['id']
    # log.info(id)
